package com.itheima.tliasmanagement.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tliasmanagement.dto.AddClazzDto;
import com.itheima.tliasmanagement.dto.ClassListDto;
import com.itheima.tliasmanagement.dto.UpdateClazzDto;
import com.itheima.tliasmanagement.entity.Clazz;
import com.itheima.tliasmanagement.entity.Student;
import com.itheima.tliasmanagement.exception.BizException;
import com.itheima.tliasmanagement.mapper.ClazzMapper;
import com.itheima.tliasmanagement.mapper.StudentMapper;
import com.itheima.tliasmanagement.service.ClazzService;
import com.itheima.tliasmanagement.vo.ClazzListVo;
import com.itheima.tliasmanagement.vo.PageBean;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public void deleteById(Integer id) {
        Clazz clazzDB = clazzMapper.selectById(id);
        if(clazzDB == null){
            throw new BizException("班级不存在");
        }
        // 判断是否有学生
        List<Student> students = studentMapper.selectByClazzId(id);
        if(!CollectionUtils.isEmpty(students)){
            throw new BizException("班级有学生，无法删除");
        }
        clazzMapper.deleteById(id);
    }

    @Override
    public void updateClazz(UpdateClazzDto dto) {
        String name = dto.getName();
        // 判断name是否重复
        Clazz clazzDB = clazzMapper.selectByName(name);
        if(clazzDB == null){
            // ok
        }else if(Objects.equals(dto.getId(), clazzDB.getId())){
            // ok
        }else {
            throw new BizException("班级名称重复");
        }
        // 更新
        Clazz clazz = new Clazz();
        BeanUtils.copyProperties(dto, clazz);
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }

    @Override
    public Clazz getById(Integer id) {
        Clazz clazz = clazzMapper.selectById(id);
        return clazz;
    }

    @Override
    public void add(AddClazzDto dto) {
        String name = dto.getName();
        // 判断name是否重复
        Clazz clazzDB = clazzMapper.selectByName(name);
        if(clazzDB != null){
            throw new BizException("班级名称重复");
        }
        // 新增
        Clazz clazz = new Clazz();
        BeanUtils.copyProperties(dto, clazz);
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insert(clazz);
    }

    @Override
    public PageBean<ClazzListVo> pageQuery(ClassListDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
//        Page<ClazzListVo> page =  clazzMapper.pageQuery(dto);
//        List<ClazzListVo> result = page.getResult();
//        if(!CollectionUtils.isEmpty(result)){
//            result.forEach(vo->{
//                vo.setStatus(getStatus(vo));
//            });
//        }
//        return new PageBean<ClazzListVo>(page.getTotal(), result);
        Page<ClazzListVo> page = clazzMapper.page(dto);
        return new PageBean<ClazzListVo>(page.getTotal(), page.getResult());
    }

    @Override
    public List<Clazz> all() {
        return clazzMapper.all();
    }

    private String getStatus(ClazzListVo vo){
        // 状态
        LocalDate now = LocalDate.now();
        LocalDate begin = vo.getBeginDate();
        LocalDate end = vo.getEndDate();
        if(now.isAfter(begin) && now.isBefore(end)){
            // 开课中
            return "已开班";
        }else if(now.isBefore(begin)){
            // 还没开课
            return "未开班";
        }else if(now.isAfter(end)){
            // 已经结课
            return "已结课";
        }else{
            return "";
        }
    }

}
